package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)//事物处理(rollbackFor = Exception.class 处理所有异常 包括IO流)
    public void save(DishDTO dishDTO) {

        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.save(dish);
        //获取当前插入的菜品id
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        //设置分页信息
        //PageHelper 会统计查询总数、自动 limit分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishes = dishMapper.findByPage(dishPageQueryDTO);

        return new PageResult(dishes.getTotal(), dishes.getResult());

    }

    @Override
    public void updateStatus(Integer status, Long id) {

        Dish dish = Dish.builder().build();
        dish.setStatus(status);
        dish.setId(id);

        dishMapper.update(dish);

    }

    @Override
    public DishDTO findById(Long id) {
        DishDTO dishDTO = dishMapper.findById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.findByDishId(id);
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DishDTO dishDTO) {

        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        Long dishId = dish.getId();
        dishFlavorMapper.deleteByDishId(dishId);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(List<Long> ids) {

        for (Long id : ids) {
            DishDTO dishDTO = dishMapper.findById(id);
            //查看当前菜品是否在售
            if (dishDTO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setMealIds = setMealDishMapper.getSetMealIdsByDishIds(ids);
        if (setMealIds != null && setMealIds.size() > 0) {
            //当前菜品被套餐关联了，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表中的菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);//后绪步骤实现
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);//后绪步骤实现
        }

    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    @Override
    public List<DishVO> listDishVO(Dish dish) {
        //通过种类拿到对应的菜品
        List<Dish> dishes = dishMapper.findByCategoryId(dish);

        ArrayList<DishVO> dishVOS = new ArrayList<>();
        for (Dish dish1 : dishes) {
            DishVO dishVO = DishVO.builder().build();
            BeanUtils.copyProperties(dish1, dishVO);

            dishVOS.add(dishVO);
            Long dishId = dish1.getId();
            List<DishFlavor> flavors = dishFlavorMapper.findByDishId(dishId);
            if (flavors.size() != 0) {
                dishVO.setFlavors(flavors);
            }
        }
        return dishVOS;
    }
}
